/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.controler;

import Utils.ValidateFocusLost;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import samoloty.model.IDataModel;
import samoloty.view.FlightPanel;
import samoloty.view.MainWindow;
import samoloty.view.MyButton;

/**
 *
 * @author borysfan
 */
public class FlightPanelControler implements MyControler {
    
    private FlightPanel panel;
    private Controler controler;
    private IDataModel dataModel;

    public FlightPanelControler(Controler c) {
        controler = c;
        initPanel();
    }

    public FlightPanelControler(Controler c, IDataModel data) {
        controler = c;
        dataModel = data;
        initPanel();
    }

    public FlightPanel getPanel() {
        return panel;
    }

    public void initPanel() {
        panel = new FlightPanel();
        panel.setYearActionListener(new ChangeYearListener());
        panel.setTimeActionListener(new ChangeTimeListener());
        panel.addFocusLostListener_destAppText( new ValidateFocusLost() );
        panel.addFocusLostListener_fltnoText( new ValidateFocusLost() );
        panel.addFocusLostListener_origAptText( new ValidateFocusLost() );
        panel.addFocusLostListener_prepareText( new ValidateFocusLost() );
    }

    public void setUpWindow() {
        MainWindow window = controler.getView();

        window.setLeftPanelVisible(true);
        window.setRightPanelVisible(true);
        window.setAllBtnStyle(MyButton.ACTIVE_BUTTON);
        window.setFlightBtnStyle(MyButton.CHOOSE_BUTTON);
        firstFocused();
        controler.getPlotPanelControler().update();
    }

    public void reset() {
        panel.reset();
    }

    public String getVPreparedBy() {
        return panel.getVPrepareBy();
    }

    public String getVOrigin() {
        return panel.getVOrigin();
    }

    public String getVDestination() {
        return panel.getVDestination();
    }

    public String getVFlightNumber() {
        return panel.getVFlightNumber();
    }

    public String getVDay() {
        return panel.getVDay();
    }

    public String getVMonth() {
        return panel.getVMonth();
    }

    public String getVYear() {
        return panel.getVYear();
    }

    public void setVYear(String vYear) {
        panel.setVyear(vYear);
    }

    public String getVTime() {
        return panel.getVTime();
    }

    public void setVTime(String vTime) {
        panel.setVtime(vTime);
    }

    public void firstFocused() {
        panel.setFirstFieldFocus();
    }

    private class ChangeYearListener implements FocusListener {

        Calendar calendar = new GregorianCalendar();

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            String vYear = getVYear();
            if (vYear.length() < 4) {
                setVYear(calendar.get(Calendar.YEAR) + "");
            }
        }
    }

    private class ChangeTimeListener implements FocusListener {

        public void focusGained(FocusEvent fe) {
        }

        public void focusLost(FocusEvent fe) {
            String vTime = getVTime();
            if (vTime.length() == 4) {
                String vHou = vTime.substring(0, 2);
                String vMin = vTime.substring(2, 4);
                System.out.println(vHou + ":" + vMin);
                if (Integer.parseInt(vHou) > 23) {
                    vHou = Integer.toString(23);
                }
                if (Integer.parseInt(vMin) > 59) {
                    vMin = Integer.toString(59);
                }

                setVTime(vHou + vMin);
            }
            if (vTime.length() < 4) {
                Calendar calendar = new GregorianCalendar();
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                String hour_s = "";
                int minute = calendar.get(Calendar.MINUTE);
                String minute_s = "";
                if (hour < 10) {
                    hour_s = "0" + String.valueOf(hour);
                } else {
                    hour_s = String.valueOf(hour);
                }
                if (minute < 10) {
                    minute_s = "0" + String.valueOf(minute);
                } else {
                    minute_s = String.valueOf(minute);
                }
                setVTime(hour_s + minute_s);
            }

        }
    }


}
